using DiLu.Expansion.File.Configs;
using DiLu.Expansion.File.Operates.LocalDisk.Configs;

namespace DiLu.Expansion.File.Operates.LocalDisk;

/// <summary>
///     搜索本地磁盘文件
/// </summary>
public static class SearchLocalDiskFile
{
    /// <summary>
    ///     搜索本地磁盘文件
    /// </summary>
    /// <param name="config">搜索本地文件配置</param>
    /// <returns></returns>
    public static async IAsyncEnumerable<FileInformation> SearchAsync(this SearchLocalDiskFileConfig config)
    {
        if (!Directory.Exists(config.DirectoryFullPath)) yield break;

        var files = Directory.GetFiles(config.DirectoryFullPath, config.SearchPattern, config.SearchOption);
        foreach (var file in files)
        {
            var fileInfo = new FileInfo(file);
            if (!config.ContainsHideFile && fileInfo.Attributes.HasFlag(FileAttributes.Hidden))
            {
                await Task.Delay(0);
                continue;
            }

            yield return new FileInformation(fileInfo.Name, fileInfo.DirectoryName, fileInfo.Extension,
                MimeTypes.GetMimeType(fileInfo.Name));
            await Task.Delay(0);
        }
    }
}